package com.appkubes.mall.restful.application.user.user.service.impl;

import com.appkubes.common.exception.BusinessException;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.common.utils.data.string.UUIDUtils;
import com.appkubes.mall.core.entity.MallUser;
import com.appkubes.mall.core.entity.MallUserWxApp;
import com.appkubes.mall.restful.application.common.sms.service.CodeService;
import com.appkubes.mall.restful.application.user.extend.service.UserExtendService;
import com.appkubes.mall.restful.application.user.user.dao.UserDao;
import com.appkubes.mall.restful.application.user.user.dao.UserWxAppDao;
import com.appkubes.mall.restful.application.user.user.entity.dto.UserWxBindDTO;
import com.appkubes.mall.restful.application.user.user.entity.dto.UserWxDTO;
import com.appkubes.mall.restful.application.user.user.entity.vo.*;
import com.appkubes.mall.restful.application.user.user.service.UserService;
import com.appkubes.mall.restful.application.user.user.service.UserServiceLogin;
import com.appkubes.mall.restful.application.user.user.service.UserWxAppService;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;


@Service
@Data
public class UserServiceLoginImpl implements UserServiceLogin {

    @Autowired
    UserDao userDao;
    @Resource
    UserService userService;

    @Autowired
    UserWxAppDao userWxAppDao;

    @Autowired
    UserWxAppService userWxAppService;

    @Resource
    UserExtendService userExtendService;


    public MallUser getUserByTel(String tel) {
        MallUser user = userDao.selectByTel(tel);
        if (user == null) {
            //自动注册
            user = new MallUser();
            user.setTel(tel);
            userDao.insert(user);
            //注册fintech客户
            addFintechUser(user);
        }
        user.setRegisterType(UUIDUtils.getUUID());
        userDao.updateById(user);
        return user;
    }

    private void addFintechUser(MallUser user) {
        // 注册fintech客户列表
        UserRegisterVO userRegisterVO = new UserRegisterVO();
        userRegisterVO.setPassword(user.getPassword());
        userRegisterVO.setTel(user.getTel());
        userService.addFintechUserInfo(user, userRegisterVO);
    }

    public String add(MallUser model) {
        model.setUserKey(UUIDUtils.getUUID());
        model.setCreateTime(DateUtils.DateToTimestamp(new Date()));
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        model.setAppKey(null);
        userService.save(model);
        return model.getUserKey();
    }


    @Override
    public UserWxDTO loginByWxApp(MallUserWxVO userWxVO) {
        UserWxDTO loginDTO = new UserWxDTO();
        MallUserWxApp userWx = userWxAppDao.selectById(userWxVO.getWxAppOpenid());
        if (userWx == null) {
            //添加微信信息
            userWx = new MallUserWxApp();
            BeanUtils.copyProperties(userWxVO, userWx);
            userWx.setAppKey(null);
            userWxAppService.save(userWx);
        } else {
            BeanUtils.copyProperties(userWxVO, userWx);
            userWxAppService.updateById(userWx);
        }

        BeanUtils.copyProperties(userWx, loginDTO);

        if (!StringUtils.isEmpty(loginDTO.getBindUserKey())) {
            MallUser mallUser = userService.getById(loginDTO.getBindUserKey());
            mallUser.setRegisterType(UUIDUtils.getUUID());
            loginDTO.setAvatarUrl(mallUser.getImg());
            loginDTO.setNickName(mallUser.getName());
            loginDTO.setTel(mallUser.getTel());
            loginDTO.setToken(mallUser.getRegisterType());
            //每次登陆更新ClientId
            userExtendService.saveClientId(mallUser.getUserKey(), userWxVO.getClientId());
            userService.updateById(mallUser);
        }

        return loginDTO;
    }


    @Override
    @Transactional
    public UserWxBindDTO bindTel(WxUserBindTelVO userBindTelVO) throws BusinessException {
        UserWxBindDTO userWxBindDTO = new UserWxBindDTO();

        MallUserWxApp userWxApp = userWxAppService.getById(userBindTelVO.getWxAppOpenid());
        if (userWxApp == null) {
            throw new BusinessException("未找到该微信用户");
        }

        MallUser user = userDao.selectByTel(userBindTelVO.getTel());
        if (user == null) {
            //生成平台用户
            user = new MallUser();
            user.setAppKey(userBindTelVO.getAppKey());
            user.setTel(userBindTelVO.getTel());
            user.setUserKey(UUIDUtils.getUUID());
            user.setName(userWxApp.getNickName());
            user.setImg(userWxApp.getAvatarUrl());
            user.setCreateTime(DateUtils.DateToTimestamp(new Date()));
            user.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
            user.setRegisterType(UUIDUtils.getUUID());
            user.setAppKey(null);

            addFintechUser(user);
            //每次登陆更新ClientId
            userExtendService.saveClientId(user.getUserKey(), userBindTelVO.getClientId());
        } else {
            user.setName(userWxApp.getNickName());
            user.setImg(userWxApp.getAvatarUrl());
            user.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
            user.setRegisterType(UUIDUtils.getUUID());
        }
        userService.saveOrUpdate(user);

        userWxApp.setBindUserKey(user.getUserKey());
        userWxAppService.updateById(userWxApp);

        userWxBindDTO.setUserKey(user.getUserKey());
        userWxBindDTO.setWxAppOpenid(userBindTelVO.getWxAppOpenid());
        userWxBindDTO.setToken(user.getRegisterType());

        return userWxBindDTO;
    }

    @Override
    public MallUser loginByTel(LoginByTelVO loginByTelVO) {
        MallUser user = getUserByTel(loginByTelVO.getTel());
        //每次登陆更新ClientId
        userExtendService.saveClientId(user.getUserKey(), loginByTelVO.getClientId());
        return user;
    }

    @Override
    public MallUser loginByTelWx(LoginByTelWxVO loginByTelWxVO) {
        MallUser user = getUserByTel(loginByTelWxVO.getTel());
        return user;
    }

    @Override
    public Boolean modifyUserInfo(ModifyUserVo modifyUserVo) {
        MallUser mallUser = userService.getById(modifyUserVo.getUserKey());
        if (mallUser == null) return false;
        mallUser.setImg(modifyUserVo.getAvatarUrl());
        mallUser.setName(modifyUserVo.getNickName());
        return userService.updateById(mallUser);
    }

}
